package com.wuliu.quartz.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuliu.system.domain.SysPushMsg;
import com.wuliu.system.domain.SysSmsRecord;
import com.wuliu.system.domain.TolPush;
import com.wuliu.system.service.ISysPushMsgService;
import com.wuliu.system.service.ISysSmsRecordService;
import com.wuliu.system.service.ITolPushService;
import com.wuliu.system.service.WeixinService;
import org.springframework.stereotype.Component;
import com.wuliu.common.utils.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
    @Resource
    private ISysSmsRecordService sysSmsRecordService;
    @Resource
    private ITolPushService tolPushService;
    @Resource
    private WeixinService weixinService;
    @Resource
    private ISysPushMsgService sysPushMsgService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        System.out.println("执行无参方法");
    }

    /**
     * 推送小程序公众号消息
     */
    public void sendWxMessage() {
        List<TolPush> tolPushes = tolPushService.selectTolPushToSend();
        tolPushes.forEach(t -> weixinService.pushWxMessage(t));
    }

    /**
     * 发送短信
     */
    public void sendSms() {
//        List<SysSmsRecord> smsRecords = sysSmsRecordService.list(new LambdaQueryWrapper<SysSmsRecord>()
//                .eq(SysSmsRecord::getStatus, 0)
//                .orderByDesc(SysSmsRecord::getCreateTime)
//                .last("limit 50"));
//        smsRecords.forEach(t -> sysSmsRecordService.sendSms(t));
    }

    /**
     * 推送app消息
     */
    public void pushAppSms() {
        List<SysPushMsg> sysPushMsgs = sysPushMsgService.list(new LambdaQueryWrapper<SysPushMsg>()
                .eq(SysPushMsg::getPushFlag, 0)
                .orderByDesc(SysPushMsg::getCreateTime)
                .last("limit 50"));
        Map<String, List<SysPushMsg>> map = sysPushMsgs.stream().collect(Collectors.groupingBy(SysPushMsg::getSysAccount));
        for (Map.Entry<String, List<SysPushMsg>> entry : map.entrySet()) {
            sysPushMsgService.sysPushMsgs(entry.getKey(), entry.getValue());
        }
    }
}
